﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace V5_CMS.Filters {

    [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
    public class ExceptionFilter : HandleErrorAttribute {
        public override void OnException(ExceptionContext filterContext) {
            StringBuilder sbuilder = new StringBuilder();
            sbuilder.Append(Environment.NewLine + "==========全局异常捕捉=========" + Environment.NewLine);
            sbuilder.Append(filterContext.Exception.Message.ToString() + Environment.NewLine);
            sbuilder.Append(filterContext.Exception.StackTrace.ToString() + Environment.NewLine);
            sbuilder.Append("全局异常结束" + Environment.NewLine);

            string message = string.Format("消息类型：{0}\n消息内容：{1}\n引发异常的方法：{2}\n引发异常的对象：{3}\n异常目录：{4}\n异常文件：{5}"
              , filterContext.Exception.GetType().Name
              , filterContext.Exception.Message
              , filterContext.Exception.TargetSite
              , filterContext.Exception.Source
              , filterContext.RouteData.GetRequiredString("controller")
              , filterContext.RouteData.GetRequiredString("action"));

            filterContext.HttpContext.Response.Write(message);
            filterContext.HttpContext.Response.End();
        }
    }
}